<include file="header"/>
<include file="commonJs"/>
<div class="wraper">
    <div class="operate">
        <p class="operate-theme pull-left">购物车（<span>{BEESCRM:$num}</span>）</p>
        <a href="javascript:;" class="operate-delete pull-left">删除</a>
        <a href="javascript:;" class="operate-collect pull-left">加入收藏夹</a>
        <input type="hidden" id="checked-type" value="">
    </div>
    <!--自营快递-->
    <if condition="$selfData['kd']|count gt 0">
    <div data-type="kd_self" class="data_type">
        <p class="block-space"><span class="iconfont icon-checkbox checked-type"></span> 同力自营</p>
        <volist name="selfData['kd']" id="vo">
        <div class="cart-list clearfix">
            <div class="list-info check-item-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}">
                <div class="checkbox">
                    <span class="iconfont icon-checkbox checked-goods"></span>
                </div>
                <!--<div class="product-img"><img src="{{goods_image}}"/></div>-->
                <div class="product-img"><div><img src="{BEESCRM:$vo.goods_image}"/></div></div>
                <div class="product-info">
                    <p>{BEESCRM:$vo.goods_name}</p>
                    <p class="prize text-12">
                        <if condition="$vo.discount gt 0">
                            <span class='product-label label-bg-red'>{BEESCRM:$vo.discount}折</span>
                        </if>
                        <if condition="$vo.picked eq 1">
                            <span class='product-label'>自提</span>
                        <else/>
                            <span class='product-label'>快递</span>
                        </if>
                    </p>
                    <if condition="$vo.specification">
                        <p class="specification">规格：{BEESCRM:$vo.specification}</p>
                    </if>
                </div>
            </div>
            <div class="cart-detail checkbox clearfix">
                <span class="new-prize">￥<span class="goods_price">{BEESCRM:$vo.goods_price}</span></span>

                <div class="quantity pull-right">
                        <span class="iconfont icon-iconfont55 change-num-btn  sub-btn-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}"
                              data-status="sub"></span>
                    <input type="number" value="{BEESCRM:$vo.goods_num}" class="buy-count buy-count-{BEESCRM:$vo.cart_id}"
                           data-id="{BEESCRM:$vo.cart_id}" data-status="input"/>
                        <span class="iconfont icon-jia change-num-btn plus-btn-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}"
                              data-status="plus"></span>
                </div>
            </div>
        </div>
        </volist>
    </div>
    </if>
    <!--自营自提-->
    <if condition="$selfData['zt']|count gt 0">
        <div data-type="zt_self" class="data_type">
            <p class="block-space"><span class="iconfont icon-checkbox checked-type"></span> 同力自营</p>
            <volist name="selfData['zt']" id="vo">
                <div class="cart-list clearfix">
                    <div class="list-info check-item-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}">
                        <div class="checkbox">
                            <span class="iconfont icon-checkbox checked-goods"></span>
                        </div>
                        <!--<div class="product-img"><img src="{{goods_image}}"/></div>-->
                        <div class="product-img"><div><img src="{BEESCRM:$vo.goods_image}"/></div></div>
                        <div class="product-info">
                            <p>{BEESCRM:$vo.goods_name}</p>
                            <p class="prize text-12">
                                <if condition="$vo.discount gt 0">
                                    <span class='product-label label-bg-red'>{BEESCRM:$vo.discount}折</span>
                                </if>
                                <if condition="$vo.picked eq 1">
                                    <span class='product-label'>自提</span>
                                    <else/>
                                    <span class='product-label'>快递</span>
                                </if>
                            </p>
                            <if condition="$vo.specification">
                                <p class="specification">规格：{BEESCRM:$vo.specification}</p>
                            </if>
                        </div>
                    </div>
                    <div class="cart-detail checkbox clearfix">
                        <span class="new-prize">￥<span class="goods_price">{BEESCRM:$vo.goods_price}</span></span>

                        <div class="quantity pull-right">
                        <span class="iconfont icon-iconfont55 change-num-btn  sub-btn-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}"
                              data-status="sub"></span>
                            <input type="number" value="{BEESCRM:$vo.goods_num}" class="buy-count buy-count-{BEESCRM:$vo.cart_id}"
                                   data-id="{BEESCRM:$vo.cart_id}" data-status="input"/>
                        <span class="iconfont icon-jia change-num-btn plus-btn-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}"
                              data-status="plus"></span>
                        </div>
                    </div>
                </div>
            </volist>
        </div>
    </if>

    <!-- 厂家直营 -->
    <volist name="otherData" id="v" key="k">
        <!--直营快递-->
        <if condition="$v['kd']|count gt 0">
            <div data-type="kd_other_{BEESCRM:$k}" class="data_type">
                <p class="block-space"><span class="iconfont icon-checkbox checked-type"></span> 厂家直营</p>
                <volist name="v['kd']" id="vo">
                    <div class="cart-list clearfix">
                        <div class="list-info check-item-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}">
                            <div class="checkbox">
                                <span class="iconfont icon-checkbox checked-goods"></span>
                            </div>
                            <!--<div class="product-img"><img src="{{goods_image}}"/></div>-->
                            <div class="product-img"><div><img src="{BEESCRM:$vo.goods_image}"/></div></div>
                            <div class="product-info">
                                <p>{BEESCRM:$vo.goods_name}</p>
                                <p class="prize text-12">
                                    <if condition="$vo.discount gt 0">
                                        <span class='product-label label-bg-red'>{BEESCRM:$vo.discount}折</span>
                                    </if>
                                    <if condition="$vo.picked eq 1">
                                        <span class='product-label'>自提</span>
                                        <else/>
                                        <span class='product-label'>快递</span>
                                    </if>
                                </p>
                                <if condition="$vo.specification">
                                    <p class="specification">规格：{BEESCRM:$vo.specification}</p>
                                </if>
                            </div>
                        </div>
                        <div class="cart-detail checkbox clearfix">
                            <span class="new-prize">￥<span class="goods_price">{BEESCRM:$vo.goods_price}</span></span>

                            <div class="quantity pull-right">
                        <span class="iconfont icon-iconfont55 change-num-btn  sub-btn-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}"
                              data-status="sub"></span>
                                <input type="number" value="{BEESCRM:$vo.goods_num}" class="buy-count buy-count-{BEESCRM:$vo.cart_id}"
                                       data-id="{BEESCRM:$vo.cart_id}" data-status="input"/>
                        <span class="iconfont icon-jia change-num-btn plus-btn-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}"
                              data-status="plus"></span>
                            </div>
                        </div>
                    </div>
                </volist>
            </div>
        </if>

        <!--直营自提-->
        <if condition="$v['zt']|count gt 0">
            <div data-type="zt_other_{BEESCRM:$k}" class="data_type">
                <p class="block-space"><span class="iconfont icon-checkbox checked-type"></span> 厂家直营</p>
                <volist name="v['zt']" id="vo">
                    <div class="cart-list clearfix">
                        <div class="list-info check-item-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}">
                            <div class="checkbox">
                                <span class="iconfont icon-checkbox checked-goods"></span>
                            </div>
                            <!--<div class="product-img"><img src="{{goods_image}}"/></div>-->
                            <div class="product-img"><div><img src="{BEESCRM:$vo.goods_image}"/></div></div>
                            <div class="product-info">
                                <p>{BEESCRM:$vo.goods_name}</p>
                                <p class="prize text-12">
                                    <if condition="$vo.discount gt 0">
                                        <span class='product-label label-bg-red'>{BEESCRM:$vo.discount}折</span>
                                    </if>
                                    <if condition="$vo.picked eq 1">
                                        <span class='product-label'>自提</span>
                                        <else/>
                                        <span class='product-label'>快递</span>
                                    </if>
                                </p>
                                <if condition="$vo.specification">
                                    <p class="specification">规格：{BEESCRM:$vo.specification}</p>
                                </if>
                            </div>
                        </div>
                        <div class="cart-detail checkbox clearfix">
                            <span class="new-prize">￥<span class="goods_price">{BEESCRM:$vo.goods_price}</span></span>

                            <div class="quantity pull-right">
                                <span class="iconfont icon-iconfont55 change-num-btn  sub-btn-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}"
                                      data-status="sub"></span>
                                <input type="number" value="{BEESCRM:$vo.goods_num}" class="buy-count buy-count-{BEESCRM:$vo.cart_id}"
                                       data-id="{BEESCRM:$vo.cart_id}" data-status="input"/>
                                <span class="iconfont icon-jia change-num-btn plus-btn-{BEESCRM:$vo.cart_id}" data-id="{BEESCRM:$vo.cart_id}"
                                      data-status="plus"></span>
                            </div>
                        </div>
                    </div>
                </volist>
            </div>
        </if>
    </volist>

<div class="amount">已选 <span class="amount-num">0</span> 件商品</div>

<div class="fixed-accounts ">
<div class="tb-mode">
    <!--
    <div class="checkbox tb-cell describ-gray checked-all">
        <span class="iconfont icon-checkbox"></span>
        <span>全选</span>
    </div>
    -->
    <div class="total tb-celll">
        <p class="text-right">合计：<span class="describ-red" id="cost-total">0.00</span></p>

        <p class="describ-gray text-right">不含运费</p>
    </div>
    <div class="amount-btn tb-cell">
        <a class="describ-white btn" href="javascript:;">结算（<span class="amount-num">0</span>）</a>
    </div>
</div>
</div>
</div>
</div>

<script type="text/javascript">
$(function(){
    //单选
    $('.list-info').click(function(){
        var id = $(this).data('id');

        var type = $(this).parents('.data_type').data('type');

        if ($('#checked-type').val() != '' && $('#checked-type').val() != type){
            $.alert('error', '您已选中了一类商品，请先完成支付');
            return false;
        }else{
            $('#checked-type').val(type);
        }

        if ($(this).find('.checked-goods').hasClass('icon-xuanzedianjihou')){
            $(this).find('.checked-goods').removeClass('icon-xuanzedianjihou');
            $(this).find('.checked-goods').removeClass('color-red');
        }else{
            $(this).find('.checked-goods').addClass('icon-xuanzedianjihou');
            $(this).find('.checked-goods').addClass('color-red');
        }

        //判断是否已经全部选中
        var num = $(this).parents('.data_type').find('.checked-goods').length;
        var checked_num = $(this).parents('.data_type').find('.icon-xuanzedianjihou').not('.checked-type').length;

        if (checked_num == 0){
            $('#checked-type').val('');
        }
        if (checked_num == num){
            $(this).parents('.data_type').find('.checked-type').addClass('icon-xuanzedianjihou');
            $(this).parents('.data_type').find('.checked-type').addClass('color-red');
        }else {
            $(this).parents('.data_type').find('.checked-type').removeClass('icon-xuanzedianjihou');
            $(this).parents('.data_type').find('.checked-type').removeClass('color-red');
        }
        showGoodsPrize();
    });
    //  全选
    $('.checked-type').click(function(){
        var type = $(this).parent().parent().data('type');

        if ($(this).hasClass('icon-xuanzedianjihou')){
            $(this).parent().parent().find('.icon-checkbox').removeClass('icon-xuanzedianjihou');
            $(this).parent().parent().find('.icon-checkbox').removeClass('color-red');
            $('#checked-type').val('');
        }else{
            if ($('#checked-type').val() != '' && $('#checked-type').val() != type){
                $.alert('error', '您已选中了一类商品，请先完成支付');
                return false;
            }else{
                $('#checked-type').val(type);
            }
            $(this).parent().parent().find('.icon-checkbox').addClass('icon-xuanzedianjihou');
            $(this).parent().parent().find('.icon-checkbox').addClass('color-red');
        }
        showGoodsPrize();
    });

    //显示选择数量和金额
    function showGoodsPrize(){
        var goods_num = 0;
        var goods_prize = 0;
        var this_num = 0;
        var this_prize = 0;
        $('.icon-xuanzedianjihou').not('.checked-type').parents('.cart-list').each(function(){
            this_num = $(this).find('input').val();
            this_prize = $(this).find('.goods_price').text();
            goods_num += this_num*1;
            goods_prize += this_num*this_prize;
        });
        if (goods_prize >= 1) {
            goods_prize = goods_prize * 100;
            goods_prize = goods_prize.toString();
            var len = goods_prize.length;
            var str1 = goods_prize.substr(0, len - 2);
            var str2 = goods_prize.substr(len - 2);
            var prize = str1 + '.' + str2;
        }else if (goods_prize == 0){
            prize = '0.00';
        }else{
            prize = goods_prize;
        }

        $('.amount-num').text(goods_num);
        $('#cost-total').text(prize);
    }

    //数量控制
    $('.change-num-btn').click(function(){
        var id = $(this).data('id');
        var status = $(this).data('status');
        var num = $(this).parent().find('input').val();
        renderNumVal(id, status, num);
    });

    $('.buy-count').change(function(){
        var id = $(this).data('id');
        var status = $(this).data('status');
        var num = $(this).val();
        renderNumVal(id, status, num);
    });

    function renderNumVal(id, status, num) {
        if (status == 'plus') {
            num++;
        } else if (status == 'sub') {
            if (num <= 1) {
                num = 1;
                return false;
            }
            num--;
        } else if (status == 'input') {
            num = $('.buy-count-' + id + '').val();
            if (num <= 1) {
                num = 1;
                $('.buy-count-' + id + '').val(num);
            }
        }
        $('.buy-count-' + id + '').val(num);

        $.get('/base/storeZT/updatecartnum/store_id/{BEESCRM:$storeId}', {
            cart_id: id,
            goods_num: num
        });

        showGoodsPrize();
    }

    //收藏
    $('.operate-collect').click(function(){
        if ($('.icon-xuanzedianjihou').not('.checked-type').length <= 0) {
            $.alert('error', '未选中商品');
            return false;
        }

        var checkedList = new Array();
        $i = 0;
        $('.icon-xuanzedianjihou').not('.checked-type').each(function(){
            checkedList[$i] = $(this).parent().parent().data('id');
            $i++;
        });

        $.getAjax({
            url: '/base/storeZT/movetocollection/store_id/{BEESCRM:$storeId}',
            method: 'POST',
            data: {cart_id: checkedList}
        }, true);
    });

    //删除
    $('.operate-delete').click(function(){
        if ($('.icon-xuanzedianjihou').not('.checked-type').length <= 0) {
            $.alert('error', '您未选择删除的商品');
            return false;
        }

        var checkedList = new Array();
        $i = 0;
        $('.icon-xuanzedianjihou').not('.checked-type').each(function(){
            checkedList[$i] = $(this).parent().parent().data('id');
            $i++;
        });

        $.getAjax({
            url: '/base/storeZT/deletefromcart/store_id/{BEESCRM:$storeId}',
            method: 'POST',
            data: {cart_id: checkedList}
        }, false, function (res) {
            var data = res;
            if (data.status === 'S') {
                //pageData = data.data;
                //elem.html(pageTpl({pageData: pageData}));
                $('.icon-xuanzedianjihou').not('.checked-type').parents('.cart-list').remove();
                $('.data_type').each(function(){
                   if ($(this).find('.cart-list').length == 0){
                       $(this).remove();
                   }
                });
                showGoodsPrize();
                var num = $('.cart-list').length;
                $('.operate-theme span').text(num);
                if ($('.icon-xuanzedianjihou').not('.checked-type').length <= 0) {
                    $('#checked-type').val('');
                }
            } else {
                alert('error', data.message);
            }
        });
    });

    $('.amount-btn').click(function(){
        if ($('.icon-xuanzedianjihou').not('.checked-type').length <= 0) {
            $.alert('error', '您未选择购买的商品');
            return false;
        }

//        var checkedList = [];
        var i = 0;
        var checkString = '';
        $('.icon-xuanzedianjihou').not('.checked-type').each(function(){
//            checkedList[i] = {};
//            checkedList[i]['cart_id'] = $(this).parent().parent().data('id');
            checkString +=  $(this).parent().parent().data('id') + '_';
            i++;
        });
        checkString = checkString.substring(0,checkString.length -1);
        var redirect_url = "/index.php?g=base&m=storeZT&a=confirmOrder&cart_id="+checkString+"&store_id="+"{BEESCRM:$storeId}";
        location.href = redirect_url;
//        $.getAjax({
//            url: '/base/storeZT/createorder/store_id/{BEESCRM:$storeId}',
//            method: 'POST',
//            data: {cart: checkedList, order_amount: $('#cost-total').text() }
//        }, true,redirect);
    });

    function redirect(data){
        location=data.url;
    }
});
</script>
<include file="footer"/>